package cn.roadtrip.utilmodule.util;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DataBean<T> {
    private String code = "suc";
    private String msg = "成功";
    private boolean suc = true;
    private String token = null;
    private T result;
    private Integer totalCount = 0;
    private long createTime = System.currentTimeMillis();
    private String ext_msg = "";
    private java.util.Map<String, Object> otherProperties;

    public void initSuc() {
        this.setSuc(true);
        this.setMsg("成功");
        this.setCode("suc");
    }

    public void initErr() {
        this.setSuc(false);
        this.setMsg("失败");
        this.setCode("err");
    }

    public static <R> DataBean<R> suc(R rs, Integer size, String code, String msg) {
        DataBean<R> db = new DataBean();
        db.initSuc();
        db.setCode(code);
        db.setMsg(msg);
        db.setResult(rs);
        db.setTotalCount(size);
        return db;
    }

    public static DataBean sucEmpty() {
        return DataBean.suc(new ArrayList(), 0, "list-empty", "查询列表为空");
    }

    public static DataBean errEmpty() {
        return DataBean.err("obj-null", "对象不存在或已删除");
    }

    public static <R> DataBean<R> suc(R rs, Integer size, String code, String msg, String othermsg) {
        DataBean<R> db = new DataBean();
        db.initSuc();
        db.setCode(code);
        db.setMsg(msg);
        db.setResult(rs);
        db.setExt_msg(othermsg);
        db.setTotalCount(size);
        return db;
    }

    public static DataBean err(String code, String msg) {
        DataBean db = new DataBean();
        db.initErr();
        db.setCode(code);
        db.setMsg(msg);
        return db;
    }

    public static DataBean err(String code, String msg, String othermsg) {
        DataBean db = new DataBean();
        db.initErr();
        db.setCode(code);
        db.setMsg(msg);
        db.setExt_msg(othermsg);
        return db;
    }

    public  DataBean reSetValue(Object value){
        DataBean db = new DataBean();
        db.setResult(value);
        db.setCode(this.getCode());
        db.setMsg(this.getMsg());
        db.setSuc(this.isSuc());
        db.setToken(this.getToken());
        db.setTotalCount(this.getTotalCount());
        db.setCreateTime(this.getCreateTime());
        db.setExt_msg(this.getExt_msg());
        db.setOtherProperties(this.getOtherProperties());
        return db;
    }
}